package com.yicong.taxi.api.observers;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.google.gson.JsonSyntaxException;
import com.hjq.toast.ToastUtils;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.yicong.taxi.R;
import com.yicong.taxi.api.entry.EntryListResult;
import com.yicong.taxi.utils.AppManagerUtil;


import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

/**
 * 作者:CYY
 * 时间:2018/11/28
 */
public abstract class EntryListResultObserver<T> implements Observer<EntryListResult<T>> {
    private Activity mActivity;
    private KProgressHUD mProgressHUD;
    private String loadMessage="加载中…";

    public EntryListResultObserver(Activity activity) {
        mActivity = activity;
    }

    @Override public void onSubscribe(Disposable d) {
        showLoadingProgress(loadMessage);
    }

    @Override public void onNext(EntryListResult<T> tEntryListResult) {
        if(mActivity.getString(R.string.success).equals(tEntryListResult.getCode())){
            onSuccess(tEntryListResult.getResult());
        }else if(mActivity.getString(R.string.nologin).equals(tEntryListResult.getCode())){
            AppManagerUtil.getAppManager().finishAllActivity();
//            mActivity.startActivity(new Intent(mActivity,LoginActivity.class));
            ToastUtils.show("登录信息己失效,请重新登入");
        }else{
            ToastUtils.show(tEntryListResult.getMsg());
        }
        closeLoadingProgress();
    }

    @Override public void onError(Throwable e) {
        if (e instanceof HttpException) {
            ToastUtils.show(mActivity.getResources().getString(R.string.httpexception));
        } else if (e instanceof ConnectException || e instanceof SocketTimeoutException) {
            ToastUtils.show(mActivity.getResources().getString(R.string.timeout));
        } else if (e instanceof JSONException || e instanceof ParseException) {
            ToastUtils.show(mActivity.getResources().getString(R.string.jsonexception)+e);
        } else if (e instanceof UnknownHostException) {
            ToastUtils.show(mActivity.getResources().getString(R.string.unknownhost));
        } else if(e instanceof JsonSyntaxException){
            ToastUtils.show(mActivity.getResources().getString(R.string.jsonsyntaxexception));
        }else {
            if (!TextUtils.isEmpty(e.getMessage())) {
                ToastUtils.show(e+"");
            } else {
            }
        }

        closeLoadingProgress();
    }

    @Override public void onComplete() {
        closeLoadingProgress();
    }

    public abstract void onSuccess(List<T> tList);

    //显示加载框
    public void showLoadingProgress(final String label) {
        mActivity.runOnUiThread(() -> mProgressHUD = KProgressHUD.create(mActivity)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel(label)
                //.setCancellable(false)//点击返架取消
                .setGraceTime(500)
                .setSize(120, 110)
                .show());
    }

    //关闭加载框
    public void closeLoadingProgress() {
        mActivity.runOnUiThread(() -> {
            if (mProgressHUD != null) {
                mProgressHUD.dismiss();
            }
        });
    }

    public void setLoadMessage(String loadMessage) {
        this.loadMessage = loadMessage;
    }
}
